# ITALY ONLY: Immigrant attitudes
ia1 <- data61 %>% 
  filter(lamp > -51 & lamp < 51) %>% # limit to 50 day bw
  select(factor, lamp, education, gndr, agea, foreignparent,wkhtot, freehms, stfhlth, stfedu) %>% # extract relevant variables
  rename(run = lamp) # unify running variable name


ia2 <- data91IT %>% 
  filter(thirteen > -51 & thirteen < 51) %>% 
  select(factor, thirteen, education, gndr, agea, foreignparent,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = thirteen)


ia3 <- data101IT %>% 
  filter(fifteen > -51 & fifteen < 51) %>% 
  select(factor, fifteen, education, gndr, agea, foreignparent,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = fifteen)

ia1$study <- "1"
ia2$study <- "2"
ia3$study <- "3"

ITnt <- bind_rows(ia1,ia2,ia3)
hist(ITnt$run, breaks = 1000)

ITntmeta <- RDestimate(factor ~ run | study, data = ITnt, cutpoint = 0, bw = 30)
summary(ITntmeta)
p20 <- plot(ITntmeta, gran = 8, range = c(-35,35))

lowITa <- ITntmeta$ci[1,1]
upITa <- ITntmeta$ci[1,2]
coefITa <- ITntmeta$est[1]

# ITALY ONLY: Immigration attitudes
ib1 <- data62 %>% 
  filter(lamp > -51 & lamp < 51) %>% 
  select(factor, lamp, education, gndr, agea, foreignparent,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = lamp)


ib2 <- data92IT %>% 
  filter(thirteen > -51 & thirteen < 51) %>% 
  select(factor, thirteen, education, gndr, agea, foreignparent,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = thirteen)


ib3 <- data102IT %>% 
  filter(fifteen > -51 & fifteen < 51) %>% 
  select(factor, fifteen, education, gndr, agea, foreignparent,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = fifteen)

ib1$study <- "1"
ib2$study <- "2"
ib3$study <- "3"

ITon <- bind_rows(ib1,ib2,ib3)
hist(ITon$run, breaks = 1000)

ITonmeta <- RDestimate(factor ~ run | study, data = ITon, cutpoint = 0, bw = 30)
summary(ITonmeta)
p20b <- plot(ITonmeta, gran = 8, range = c(-35,35))

lowITb <- ITonmeta$ci[1,1]
upITb <- ITonmeta$ci[1,2]
coefITb <- ITonmeta$est[1]


# Cross Country: Immigrant attitudes
ma1 <- data71 %>% 
  filter(one > -51 & one < 51) %>% 
  select(factor, one, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = one)

ma2 <- data71 %>% 
  filter(two > -51 & two < 51) %>% 
  select(factor, two, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = two)

ma3 <- data71 %>% 
  filter(three > -51 & three < 51) %>% 
  select(factor, three, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = three)

ma4 <- data71 %>% 
  filter(four > -51 & four < 51) %>% 
  select(factor, four, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = four)

ma5 <- data81 %>% 
  filter(five > -51 & five < 51) %>% 
  select(factor, five, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = five)

ma6 <- data81 %>% 
  filter(six > -51 & six < 51) %>% 
  select(factor, six, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = six)

ma7 <- data81 %>% 
  filter(seven > -51 & seven < 51) %>% 
  select(factor, seven, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = seven)

ma8 <- data81 %>% 
  filter(eight > -51 & eight < 51) %>% 
  select(factor, eight, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = eight)

ma9 <- data81 %>% 
  filter(nine > -51 & nine < 51) %>% 
  select(factor, nine, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = nine)

ma10 <- data81 %>% 
  filter(ten > -51 & ten < 51) %>% 
  select(factor, ten, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = ten)

ma11 <- data81 %>% 
  filter(eleven > -51 & eleven < 51) %>% 
  select(factor, eleven, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = eleven)

ma12 <- data81 %>% 
  filter(twelve > -51 & twelve < 51) %>% 
  select(factor, twelve, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = twelve)

ma13 <- data91 %>% 
  filter(thirteen > -51 & thirteen < 51) %>% 
  select(factor, thirteen, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = thirteen)

ma14<- data91 %>% 
  filter(fourteen > -51 & fourteen < 51) %>% 
  select(factor, fourteen, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = fourteen)

ma15 <- data101 %>% 
  filter(fifteen > -51 & fifteen < 51) %>% 
  select(factor, fifteen, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = fifteen)

ma1$study <- 1
ma2$study <- 2
ma3$study <- 3
ma4$study <- 4
ma5$study <- 5
ma6$study <- 6
ma7$study <- 7
ma8$study <- 8
ma9$study <- 9
ma10$study <- 10
ma11$study <- 11
ma12$study <- 12
ma13$study <- 13
ma14$study <- 14
ma15$study <- 15

CCnt <- bind_rows(ma1,ma2,ma3,ma4,ma5,ma6,ma7,ma8,ma9,ma10,ma11,ma12,ma13,ma14,ma15)
CCnt$study <- as.character(CCnt$study)
hist(CCnt$run, breaks = 1000)

CCntmeta <- RDestimate(factor ~ run | cntry + study, data = CCnt, cutpoint = 0, bw = 30)
summary(CCntmeta)
p19 <- plot(CCntmeta, gran = 8, range = c(-35,35))

lowCCa <- CCntmeta$ci[1,1]
upCCa <- CCntmeta$ci[1,2]
coefCCa <- CCntmeta$est[1]



# Cross Country: Immigration attitudes
mb1 <- data72 %>% 
  filter(one > -51 & one < 51) %>% 
  select(factor, one, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = one)

mb2 <- data72 %>% 
  filter(two > -51 & two < 51) %>% 
  select(factor, two, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = two)

mb3 <- data72 %>% 
  filter(three > -51 & three < 51) %>% 
  select(factor, three, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = three)

mb4 <- data72 %>% 
  filter(four > -51 & four < 51) %>% 
  select(factor, four, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = four)

mb5 <- data82 %>% 
  filter(five > -51 & five < 51) %>% 
  select(factor, five, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = five)

mb6 <- data82 %>% 
  filter(six > -51 & six < 51) %>% 
  select(factor, six, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = six)

mb7 <- data82 %>% 
  filter(seven > -51 & seven < 51) %>% 
  select(factor, seven, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = seven)

mb8 <- data82 %>% 
  filter(eight > -51 & eight < 51) %>% 
  select(factor, eight, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = eight)

mb9 <- data82 %>% 
  filter(nine > -51 & nine < 51) %>% 
  select(factor, nine, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = nine)

mb10 <- data82 %>% 
  filter(ten > -51 & ten < 51) %>% 
  select(factor, ten, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = ten)

mb11 <- data82 %>% 
  filter(eleven > -51 & eleven < 51) %>% 
  select(factor, eleven, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = eleven)

mb12 <- data82 %>% 
  filter(twelve > -51 & twelve < 51) %>% 
  select(factor, twelve, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = twelve)

mb13 <- data92 %>% 
  filter(thirteen > -51 & thirteen < 51) %>% 
  select(factor, thirteen, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = thirteen)

mb14<- data92 %>% 
  filter(fourteen > -51 & fourteen < 51) %>% 
  select(factor, fourteen, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = fourteen)

mb15 <- data102 %>% 
  filter(fifteen > -51 & fifteen < 51) %>% 
  select(factor, fifteen, education, gndr, agea, foreignparent, cntry,wkhtot, freehms, stfhlth, stfedu) %>% 
  rename(run = fifteen)

mb1$study <- 1
mb2$study <- 2
mb3$study <- 3
mb4$study <- 4
mb5$study <- 5
mb6$study <- 6
mb7$study <- 7
mb8$study <- 8
mb9$study <- 9
mb10$study <- 10
mb11$study <- 11
mb12$study <- 12
mb13$study <- 13
mb14$study <- 14
mb15$study <- 15

CCon <- bind_rows(mb1,mb2,mb3,mb4,mb5,mb6,mb7,mb8,mb9,mb10,mb11,mb12,mb13,mb14,mb15)
CCon$study <- as.character(CCon$study)
hist(CCon$run, breaks = 1000)

CConmeta <- RDestimate(factor ~ run | cntry + study, data = CCon, cutpoint = 0, bw = 30)
summary(CConmeta)
p19b <- plot(CConmeta, gran = 8, range = c(-35,35))

lowCCb <- CConmeta$ci[1,1]
upCCb <- CConmeta$ci[1,2]
coefCCb <- CConmeta$est[1]



# Immigrant attitudes with covariates
##ITALY
ITntmetac <- RDestimate(factor ~ run | study + education + gndr + agea + foreignparent, data = ITnt, cutpoint = 0, bw = 30)
summary(ITntmetac)

##CROSS COUNTRY
CCntmetac <- RDestimate(factor ~ run | study + cntry + education + gndr + agea + foreignparent, data = CCnt, cutpoint = 0, bw = 30)
summary(CCntmetac)

# Immigration attitudes with covariates
##ITALY
ITonmetac <- RDestimate(factor ~ run | study + education + gndr + agea + foreignparent, data = ITon, cutpoint = 0, bw = 30)
summary(ITonmetac)

##CROSS COUNTRY
CConmetac <- RDestimate(factor ~ run | study + cntry + education + gndr + agea + foreignparent, data = CCon, cutpoint = 0, bw = 30)
summary(CConmetac)

# Immigrant attitudes without overlaps
CCnt2 <- bind_rows(ma3,ma4,ma5,ma8,ma12,ma13,ma14,ma15)
hist(CCnt2$run, breaks = 1000)

CCntmeta2 <- RDestimate(factor ~ run | cntry + study, data = CCnt2, cutpoint = 0, bw = 30)
summary(CCntmeta2)

CCntmeta2cov <- RDestimate(factor ~ run | cntry + study + education + gndr + agea + foreignparent, data = CCnt2, cutpoint = 0, bw = 30)
summary(CCntmeta2cov)

# Immigration attitudes without overlaps
CCon2 <- bind_rows(mb3,mb4,mb5,mb8,mb12,mb13,mb14,mb15)
hist(CCon2$run, breaks = 1000)

CConmeta2 <- RDestimate(factor ~ run | cntry + study, data = CCon2, cutpoint = 0, bw = 30)
summary(CConmeta2)

CConmeta2cov <- RDestimate(factor ~ run | cntry + study + education + gndr + agea + foreignparent, data = CCon2, cutpoint = 0, bw = 30)
summary(CConmeta2cov)


##Study as Flexible Form Moderator##

# Outcome 1
CCnt2$tr <- 0
CCnt2$tr[CCnt2$run > -1] <- 1
CCnt2$wt <- kernelwts(CCnt2$run, center = 0, bw = 30)
CCnt2$study <- as.numeric(CCnt2$study)

CCnt2$nooverlap <- NA
CCnt2$nooverlap[CCnt2$study == 3] <- 1
CCnt2$nooverlap[CCnt2$study == 4] <- 2
CCnt2$nooverlap[CCnt2$study == 5] <- 3
CCnt2$nooverlap[CCnt2$study == 8] <- 4
CCnt2$nooverlap[CCnt2$study == 12] <- 5
CCnt2$nooverlap[CCnt2$study == 13] <- 6
CCnt2$nooverlap[CCnt2$study == 14] <- 7
CCnt2$nooverlap[CCnt2$study == 15] <- 8

test1 <- CCnt2 %>% 
  filter(run > -31 & run < 31)

test1 <- as.data.frame(test1)

test1$cntry <- as.factor(test1$cntry)

test1$trun <- test1$run * test1$tr

test1$Order <- test1$nooverlap

test1$Event <- test1$tr

test1$Attitudes <- test1$factor



margNT <- interflex(estimator = 'linear', Y = "Attitudes", D = "Event", X = "Order", Z = c("run", "cntry", "trun"),
          weights = "wt", vcov.type = "cluster", cl = "Order",
          data = test1)
margNT

# Outcome 2
CCon2$tr <- 0
CCon2$tr[CCon2$run > -1] <- 1
CCon2$wt <- kernelwts(CCon2$run, center = 0, bw = 30)
CCon2$study <- as.numeric(CCon2$study)


CCon2$Studies_without_Overlap <- NA
CCon2$Studies_without_Overlap[CCon2$study == 3] <- 1
CCon2$Studies_without_Overlap[CCon2$study == 4] <- 2
CCon2$Studies_without_Overlap[CCon2$study == 5] <- 3
CCon2$Studies_without_Overlap[CCon2$study == 8] <- 4
CCon2$Studies_without_Overlap[CCon2$study == 12] <- 5
CCon2$Studies_without_Overlap[CCon2$study == 13] <- 6
CCon2$Studies_without_Overlap[CCon2$study == 14] <- 7
CCon2$Studies_without_Overlap[CCon2$study == 15] <- 8

test2 <- CCon2 %>% 
  filter(run > -31 & run < 31)

test2 <- as.data.frame(test2)

test2$cntry <- as.factor(test2$cntry)

test2$trun <- test2$run * test2$tr

test2$Order <- test2$Studies_without_Overlap

test2$Event <- test2$tr

test2$Attitudes <- test2$factor


margON <- interflex(estimator = 'linear', Y = "Attitudes", D = "Event", X = "Order", Z = c("run", "cntry", "trun"),
          weights = "wt", vcov.type = "cluster", cl = "Order",
          data = test2)
plot(margON, ylim = c(-0.4,0.4))


# flexible form
margON_nl <- interflex(estimator = 'kernel', Y = "Attitudes", D = "Event", X = "Order", Z = c("run", "cntry", "trun"),
                    weights = "wt", vcov.type = "cluster", cl = "Order",
                    data = test2)
margON_nl

plot(margON_nl, ylim = c(-0.4,0.4))


##Season as Flexible Form Moderator##

# Outcome 1

test1$Season <- NA

test1$Season[test1$Order == 2 | test1$Order == 5] <- 1

test1$Season[test1$Order == 1 | test1$Order == 3 |
               test1$Order == 4 | test1$Order == 6 |
               test1$Order == 7 | test1$Order == 8] <- 2



margNT2 <- interflex(estimator = 'linear', Y = "Attitudes", D = "Event", X = "Season", Z = c("run", "cntry", "trun"),
                    weights = "wt", vcov.type = "cluster", cl = "Order",
                    data = test1)
margNT2

# Outcome 2
test2$Season <- NA

test2$Season[test2$Order == 2 | test2$Order == 5] <- 1

test2$Season[test2$Order == 1 | test2$Order == 3 |
               test2$Order == 4 | test2$Order == 6 |
               test2$Order == 7 | test2$Order == 8] <- 2


margON2 <- interflex(estimator = 'linear', Y = "Attitudes", D = "Event", X = "Season", Z = c("run", "cntry", "trun"),
                    weights = "wt", vcov.type = "cluster", cl = "Order",
                    data = test2)
margON2

plot(margON2, ylim = c(-0.4,0.4))

# flexible form
margON2_nl <- interflex(estimator = 'kernel', Y = "Attitudes", D = "Event", X = "Season", Z = c("run", "cntry", "trun"),
                     weights = "wt", vcov.type = "cluster", cl = "Order",
                     data = test2)
margON2_nl

plot(margON2_nl, ylim = c(-0.4,0.4))


##Media Salience as Flexible Form Moderator##

# Outcome 1

test1$Salience <- NA

# average number of news articles per event, across German and Italian news archives
test1$Salience[test1$Order == 1] <- 142
test1$Salience[test1$Order == 2] <- 1134
test1$Salience[test1$Order == 3] <- 190
test1$Salience[test1$Order == 4] <- 108
test1$Salience[test1$Order == 5] <- 98
test1$Salience[test1$Order == 6] <- 232
test1$Salience[test1$Order == 7] <- 54
test1$Salience[test1$Order == 8] <- 19





margNT3 <- interflex(estimator = 'linear', Y = "Attitudes", D = "Event", X = "Salience", Z = c("run", "cntry", "trun"),
                     weights = "wt", vcov.type = "cluster", cl = "Order",
                     data = test1)
margNT3

# Outcome 2
test2$Salience <- NA

# average number of news articles per event, across German and Italian news archives
test2$Salience[test2$Order == 1] <- 142
test2$Salience[test2$Order == 2] <- 1134
test2$Salience[test2$Order == 3] <- 190
test2$Salience[test2$Order == 4] <- 108
test2$Salience[test2$Order == 5] <- 98
test2$Salience[test2$Order == 6] <- 232
test2$Salience[test2$Order == 7] <- 54
test2$Salience[test2$Order == 8] <- 19


margON3 <- interflex(estimator = 'linear', Y = "Attitudes", D = "Event", X = "Salience", Z = c("run", "cntry", "trun"),
                     weights = "wt", vcov.type = "cluster", cl = "Order",
                     data = test2)
margON3

plot(margON3, ylim = c(-0.4,0.4))
abline(v=455)

# flexible form
margON3_nl <- interflex(estimator = 'kernel', Y = "Attitudes", D = "Event", X = "Salience", Z = c("run", "cntry", "trun"),
                     weights = "wt", vcov.type = "cluster", cl = "Order", bw = 100,
                     data = test2)
margON3_nl

plot(margON3_nl, ylim = c(-0.4,0.4))
abline(v=455)
